﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickerClimber.Models;
using ClickerClimber.Managers;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace ClickerClimber.Scenes.SplashScreenScene
{
    public class SplashScreenScene : IGameObject, IGameObjectDrawable
    {
        private Texture2D _splashScreenTexture;
        private Texture2D _playButtonTexture;
        
        private Vector2 _playButtonPosition;
        private Rectangle _playButtonRectangle;
        private bool _isPressed;
        private bool _enabled;

        public void LoadTextures(ContentManager content)
        {
            _splashScreenTexture = content.Load<Texture2D>(GameParameters.SPLASH_SCREEN_TEXTURE_NAME);
            _playButtonTexture = content.Load<Texture2D>(GameParameters.PLAY_BUTTON_TEXTURE_NAME);
            int w = _playButtonTexture.Width * 30 / 100;
            int h = _playButtonTexture.Height * 30 / 100;
            _playButtonRectangle = new Rectangle((int)_playButtonPosition.X+w/2, (int)_playButtonPosition.Y+h/2, _playButtonTexture.Width-w, _playButtonTexture.Height-h);
        }

        public void Draw(SpriteBatch batch)
        {
            Rectangle rect = new Rectangle(0, 0, batch.GraphicsDevice.Viewport.Width, batch.GraphicsDevice.Viewport.Height);
            batch.Draw(_splashScreenTexture, rect, Color.White);

            rect = _playButtonRectangle;
            if (_isPressed)
            {
                int w = _playButtonTexture.Width * 40 / 100;
                int h = _playButtonTexture.Height * 40 / 100;
                rect.X = (int)_playButtonPosition.X+w/2;
                rect.Y = (int)_playButtonPosition.Y+h/2;
                rect.Width = _playButtonTexture.Width-w;
                rect.Height = _playButtonTexture.Height-h;
            }
            batch.Draw(_playButtonTexture, rect, Color.White);
        }

        public void Initialize()
        {
            _playButtonPosition = new Vector2(435, 435);
            _isPressed = false;
            _enabled = false;
            AudioManager.Instance.playBackgroundMusic(AudioManager.eSound.titleMusic);
        }

        public void OnClick()
        {
            if (!_enabled) return;

            Vector2 mousePosition = InputManager.Instance.GetPosition();

            if (mousePosition.X > _playButtonPosition.X && mousePosition.X < _playButtonPosition.X + _playButtonTexture.Width &&
                mousePosition.Y > _playButtonPosition.Y && mousePosition.Y < _playButtonPosition.Y + _playButtonTexture.Height)
            {
                _isPressed = true;
                _enabled = false;
                GameController.Instance.GameState = GameController.eGameState.eGameState_Init;
            }
        }

        public void Update(GameTime gameTime)
        {
            /*
            _timer += gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
            if (_timer > GameParameters.TIME_SPLASH_WAIT)
                GameController.Instance.GameState = GameController.eGameState.eGameState_Init;*/
                
        }

    #region properties

        public bool Enable
        {
            set
            {
                _enabled = value;
            }
        }

    #endregion properties

    }
}
